package com.cnpinxi.controller.web.index;

import com.cnpinxi.controller.base.BaseController;
import com.cnpinxi.def.Constant;
import com.cnpinxi.myRunTime.MyRunTimeExcption;
import com.cnpinxi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by Baggio on 2017/2/22.
 */
@RequestMapping("/")
@Controller
public class IndexContorller extends BaseController {

@Autowired
    UserService userService;
    @RequestMapping(value="/index")
    public ModelAndView index(HttpServletRequest request,HttpServletResponse response,Long activityId) {
       logger.info("=============================index==============================================>>>>>>"+activityId);
ModelAndView mov=new ModelAndView();
        mov.setViewName("web/index");
        HttpSession session = request.getSession();
        if (super.getIsMobile(request)) {
          if(activityId!=null){

           super.oauth2(request, response);
            String openId = (String) session.getAttribute(Constant.SESSION_OPEN_ID);
              try {
              /*  User user = super.getUserByOpenId(openId);
                if (user != null) {
                    session.setAttribute(Constant.SESSION_KEY_USER_LOGIN, user);
                    userService.bindDistributor(openId,activityId);
                }*/
                  userService.bindDistributor(openId,activityId);
            } catch (MyRunTimeExcption myRunTimeExcption) {
                logger.info(myRunTimeExcption.getMessage());
                myRunTimeExcption.printStackTrace();
            } catch (Exception e) {
                logger.info(e.getMessage());
                e.printStackTrace();
            }
          }
            mov.setViewName("wap/index");
        }


        return mov;

    }
    @RequestMapping(value="/aboutMe")
    public ModelAndView aboutMe(HttpServletRequest request) {
        System.out.println("=============================index==============================================>>>>>>");
ModelAndView mov=new ModelAndView();
mov.setViewName("web/aboutMe/aboutMe");

        return mov;

    }

}
